package com.qdu.dao.impl;

import com.qdu.dao.LogDao;
import com.qdu.entity.Log;
import com.qdu.entity.LogRemark;
import com.qdu.entity.LogResponse;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import static java.util.Collections.list;
import java.util.List;

/**
 * 日志实现
 *
 * @author Lenovo
 */
public class LogDaoImpl extends BaseDaoImpl implements LogDao{

    @Override
    public List<Log> selectAll() {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<Log> list=new ArrayList<>();
        
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("select * from ActLog");
            rs = ps.executeQuery();
            while(rs.next())
            {
                list.add(new Log(rs.getString(2),rs.getString(3),rs.getString(4),rs.getDate(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getInt(9)));
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public Log selectOneById(String id) {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        Log log=null;
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("select * from ActLog where Lid= ?");
            ps.setString(1, id);
            rs=ps.executeQuery();
            if(rs.next())
            {
                log = new Log(rs.getString(2),rs.getString(3),rs.getString(4),rs.getDate(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getInt(9));
            }
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(rs, ps, con);
        }
         return log;
    }

    @Override
    public int deleteLogById(String id) {
        LogRemarkDaoImpl lrdi=new LogRemarkDaoImpl();
        List<LogRemark> list=lrdi.selectAllRemarkByLogId(id);
        for(LogRemark r:list)//删除每一个评论
        {
            String rid=r.getId();//获得每个评论的编号
            lrdi.deleteRemarkById(rid);
        }
        return executeUpdate("delete from ActLog where Lid=?",id);
    }

    @Override
    public int addLog(Log log) {
        return executeUpdate("insert into ActLog(Lid,Aid,Uid,Lname,Lcontent,Limage,Lagree) values(?,?,?,?,?,?,?)",
                "1",
                log.getActivityId(),
                log.getUserid(),
                log.getName(),
                log.getContent(),
                log.getImag(),
                log.getAgree()
        );
    }

    @Override
    public int modifyLogAgree(int now) {
        return executeUpdate("update ActLog set Lagree=?",now+1);
    }

    @Override
    public List<Log> selectThree() {
         Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<Log> list=new ArrayList();
        
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("select * from ActLog offset 0 rows fetch next 3 rows only");
            rs=ps.executeQuery();
            while(rs.next())
            {
                list.add(new Log(rs.getString(2),rs.getString(3),rs.getString(4),rs.getDate(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getInt(9)));
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

}
